angular.module('U9ImHd.services')

.factory('Socket', ['$timeout', 'Connect', function($timeout, Connect) {
    var o = {},
        _socket;

    // 建立连接
    o.connect = function () {
        if (_socket) {
            return;
        }
        var address = Connect.get('address'),
            socketPort = Connect.getServeParameter('SocketPort');
        if (!address || !socketPort) {
            return;
        }
        _socket = io.connect('http://' + address + ":" + socketPort);
    };
    // 断开连接
    o.disconnect = function () {
        if (!_socket) {
            return;
        }
        _socket.emit("clientDisconnect");
        _socket = null;
    };
    //监听接收消息
    o.on = function(eventName, callback) {
        if (_socket) {
            _socket.on(eventName, callback.__ng = asyncAngularify(_socket, callback));
        }
    };
    //移除监听消息
    o.off = function(eventName, callback) {
        if (_socket) {
            _socket.removeListener(eventName, callback.__ng);
        }
    };
    //发送消息
    o.emit = function(eventName, data) {
        if (_socket) {
            _socket.emit(eventName, data);
        }
    };
    //发给自己
    o.toSelf = function(eventName, sendData) {
        if (_socket) {
            var data = {
                EventName: eventName,
                Data: sendData
            };
            _socket.emit("toSelf", data);
        }
    };
    //发给除自己的所有人
    o.toOther = function(eventName, sendData) {
        if (_socket) {
            var data = {
                EventName: eventName,
                Data: sendData
            };
            _socket.emit("toOther", data);
        }
    };
    //发给所有人，包括自己
    o.toAll = function(eventName, sendData) {
        if (_socket) {
            var data = {
                EventName: eventName,
                Data: sendData
            };
            _socket.emit("toAll", data);
        }
    };
    //加入房间
    o.joinRoom = function(roomName) {
        if (_socket) {
            var data = {
                RoomName: roomName
            };
            _socket.emit("joinRoom", data);
        }
    };
    //离开房间
    o.leaveRoom = function(roomName) {
        if (_socket) {
            var data = {
                RoomName: roomName
            };
            _socket.emit("leaveRoom", data);
        }
    };
    //向指定房间发送消息，不包括自己
    o.toRoom = function(roomName, eventName, sendData) {
        if (_socket) {
            var data = {
                RoomName: roomName,
                EventName: eventName,
                Data: sendData
            };
            _socket.emit("toRoom", data);
        }
    };
    //向指定房间发送消息，包括自己
    o.inRoom = function(roomName, eventName, sendData) {
        if (_socket) {
            var data = {
                RoomName: roomName,
                EventName: eventName,
                Data: sendData
            };
            _socket.emit("inRoom", data);
        }
    };
    
    return o;

    function asyncAngularify(socket, callback) {
        return callback ? function() {
            var args = arguments;
            $timeout(function() {
                callback.apply(socket, args);
            }, 0);
        } : angular.noop;
    }
}]);
